using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace TelliTwitter
{
   static class VoiceTranslator
   {
      // WARNING - this is nothing but a bunch of hacks and should be refactored as soon as possible.

      //Process text output
      // - reverse order
      // - @username should translate to "Jason Alexander says to Scott Cate"
      // - pauses between tweets
      // - phrase URLs differently

      public static string Translate(string content)
      {
         string output = "";
         XmlDocument document = new XmlDocument();
         document.LoadXml(content);
         XmlElement root = document.DocumentElement;
         XmlNodeList nodelist = root.SelectNodes("tweet");

         foreach (XmlNode node in nodelist)
         {
            //do some cool stuff here
            //string regex = @"<h2>(?<username>.*?)</h2>";
            //string username = "";
            //string directedto = "";
            //if (Regex.IsMatch( node.InnerXml, regex ))
            //{
            //   username = Regex.Matches(node.InnerXml, regex)[0].Groups["username"].Value.ToString();
            //}
            node.InnerXml = node.InnerXml.ToLower();
            string regex = "<span id=\"tlcreated\">(?<timeago>.*?)</span>";
            if (Regex.IsMatch(node.InnerXml, regex))
            {
               string timeago = Regex.Matches(node.InnerXml, regex)[0].Groups["timeago"].Value.ToString();
               node.InnerXml = node.InnerXml.Replace("<h2>", "<h2>" + timeago + " ");
               node.InnerXml = node.InnerXml.Replace("<span id=\"tlcreated\">" + timeago + "</span>", "");
            }
            regex = "<div id=\"tltext\">@(?<directedto>.*?):.*?";
            if (Regex.IsMatch(node.InnerXml, regex))
            {
               string directedto = Regex.Matches(node.InnerXml, regex)[0].Groups["directedto"].Value.ToString();
               node.InnerXml = node.InnerXml.Replace("<div id=\"tltext\">@" + directedto, "<div id=\"tltext\"> to " + directedto + " ...");
            }
            node.InnerXml = node.InnerXml.Replace("</h2>", " said... </h2>");

            // These all need to be moved to a config file ///////////////////////////////
            node.InnerXml = node.InnerXml.Replace("rofl", "rolling on the floor laughing");
            node.InnerXml = node.InnerXml.Replace("http://", "");
            node.InnerXml = node.InnerXml.Replace(" tt ", " tella twitter ");
            node.InnerXml = node.InnerXml.Replace(" tt.", " tella twitter.");
            node.InnerXml = node.InnerXml.Replace("tellitwitter", "tella twitter");
            node.InnerXml = node.InnerXml.Replace("teletwitter", "tella twitter");
            node.InnerXml = node.InnerXml.Replace("tinyurl", "tie knee u r l");
            node.InnerXml = node.InnerXml.Replace(" im ", " eye m ");
            node.InnerXml = node.InnerXml.Replace(" im", " eye m.");
            node.InnerXml = node.InnerXml.Replace(" xbox", " x box");
            node.InnerXml = node.InnerXml.Replace(" imho", " in my humble opinion");
            node.InnerXml = node.InnerXml.Replace(" lmao", " laughing my ass off");
            node.InnerXml = node.InnerXml.Replace(" lmfao", " laughing my ass off");
            //////////////////////////////////////////////////////////////////////////////
         }

         output = ReverseOrderString(nodelist);
         return output;
      }

      //Reverse order of input and try to put in a pause between tweets. Also get rid of all the XML - it is not needed
      private static string ReverseOrderString(XmlNodeList nodelist)
      {
         string output = "";
         foreach (XmlNode node in nodelist) {
            output = node.InnerText + "... " + output;
         }
         return output;
      }
   }
}
